'''
Created on Mar 17, 2013

@author: kevinbauer
'''
import numpy as np

class TestingData(object):
    
    def __init__(self, test):
        fileinput = open(test, 'r')
        protocol_type = {} # Type of the protocol, e.g. tcp, udp, etc. The dictionary will look like this: {'tcp': 0, 'udp:1, ...}
        service = {} # network service on the destination, e.g., http, telnet, etc. 
        flag = {} # I'm not really sure what the flag is, but is in the data set, e.g. {'SF':1, 'S0':2, ...} 
        temp = [] # initial array of the data, but it will reshaped in a multi-dimensional numpy array.
        self.type = []
        
        counter = 0
        for line in fileinput:
            # dl means delimited line.
            dl = line.split(",")
            
            # For testing purposes, when the project is finished, delete this.
            counter = counter + 1
            if counter > 1000000:
               break            
            
            # This is to map each protocol type to a unique numerical value, e.g., {'tcp':1, 'udp':2, ...}
            if dl[1] not in protocol_type.keys():
                if len(protocol_type) > 0:
                    protocol_type[dl[1]] = max(protocol_type.values()) + 1
                else: protocol_type[dl[1]] = 1
            
            # This is the same process as the protocol but for service type, e.g., {'http':1, 'telnet':2, ...}
            if dl[2] not in service.keys():
                if len(service) > 0:
                    service[dl[2]] = max(service.values()) + 1
                else:
                    service[dl[2]] = 1
                
            # this is the same proces as before, but for the flag dictionary, e.g., {'SF':1, 'S0':2, ...}
            if dl[3] not in flag.keys():
                if len(flag) > 0:
                    flag[dl[3]] = max(flag.values()) + 1
                else: 
                    flag[dl[3]] = 1
                    
            # 1 if attack, 0 is normal.  
            if dl[41] == 'normal.\n':
                dl[41] = 0
                self.type.append(0)
            else:
                dl[41]=1
                self.type.append(1)   
                 
            # Convert the string types to numerals.
            dl[1] = protocol_type[dl[1]]
            dl[2] = service[dl[2]]
            dl[3] = flag[dl[3]]
            
            for d in range(len(dl)-1):
                dl[d] = np.float(dl[d])
            
            temp.append(dl)
        
        file.close(fileinput)
        
#        print protocol_type
#        print service
#        print flag
        
        # This training array is 41 columns wide.
        self.testing = np.reshape(temp, (-1,42), 'C')
            
    
    def printData(self):
        print self.testing